<!--
  * 外包公司信息
  *
  * @Author:    zzw
  * @Date:      2024-09-23 10:25:55
  * @Copyright  jz
-->
<template>
    <!---------- 查询表单form begin ----------->
    <a-form class="smart-query-form">
        <a-row class="smart-query-form-row">
            <a-form-item label="项目ID" class="smart-query-form-item">
                <a-select
                            v-model:value="queryForm.projectId"
                            placeholder="项目ID"
                            style="width: 150px"
                            :options="projectOptions"
                          ></a-select>
            </a-form-item>
            <a-form-item label="工作日期" class="smart-query-form-item">
                <a-date-picker style="width: 150px" v-model:value="queryForm.workDay" placeholder="工作日期" value-format="YYYY-MM-DD" />
            </a-form-item>
            <a-form-item label="审批状态" class="smart-query-form-item">
                <a-select
                    v-model:value="queryForm.approvalStatus"
                    placeholder="审批状态"
                    style="width: 150px"
                    :options="[{value:0,label:'未审批'},{value:1,label:'审批通过'},{value:2,label:'审批未通过'}]"
                ></a-select>
            </a-form-item>
            <a-form-item class="smart-query-form-item">
                <a-button type="primary" @click="queryData">
                    <template #icon>
                        <SearchOutlined />
                    </template>
                    查询
                </a-button>
                <a-button @click="resetQuery" class="smart-margin-left10">
                    <template #icon>
                        <ReloadOutlined />
                    </template>
                    重置
                </a-button>
            </a-form-item>
        </a-row>
    </a-form>
    <!---------- 查询表单form end ----------->

    <a-card size="small" :bordered="false" :hoverable="true">

         <!---------- 表格操作行 begin ----------->
         <a-row class="smart-table-btn-block" style="flex-direction: row-reverse;">
            <div class="smart-table-setting-block">
                <TableOperator v-model="columns" :tableId="TABLE_ID_CONST.BUSINESS.OA.PROJECT_DAILY" :refresh="queryData" />
            </div>
        </a-row>
        <!---------- 表格操作行 end ----------->
        <a-table
            :scroll="{ x: 1300 }"
                size="small"
                :dataSource="tableData"
                :columns="columns"
                rowKey="id"
                bordered
                :loading="tableLoading"
                :pagination="false"
        >
            <template #bodyCell="{ text, record, column,index }">
                <template v-if="column.dataIndex === 'index'">
                    {{ index+1 }}
                </template>
                <template v-if="column.dataIndex === 'approvalStatus'">
                    <a-tag v-if="record.approvalStatus===1" color="success">通过</a-tag>
                    <a-tag v-if="record.approvalStatus===2" color="error">未通过</a-tag>
                    <a-tag v-if="record.approvalStatus===0" color="warning">未审批</a-tag>
                </template>
                <template v-if="column.dataIndex === 'action'">
                    <div class="smart-table-operate">
                        <a-button @click="detail(record)"  type="link">详情</a-button>
                        <a-button v-if="record.approvalStatus===2&&useUserStore().employeeId===record.creatorId" @click="updata(record)"  type="link">重新提交</a-button>
                        <a-button v-if="record.approvalStatus===0&&useUserStore().employeeId===record.approverId" @click="approval(record)" type="link">审批</a-button>
                    </div>
                </template>
            </template>
        </a-table>
        <!---------- 表格 end ----------->

        <div class="smart-query-table-page">
            <a-pagination
                    showSizeChanger
                    showQuickJumper
                    show-less-items
                    :pageSizeOptions="PAGE_SIZE_OPTIONS"
                    :defaultPageSize="queryForm.pageSize"
                    v-model:current="queryForm.pageNum"
                    v-model:pageSize="queryForm.pageSize"
                    :total="total"
                    @change="queryData"
                    @showSizeChange="queryData"
                    :show-total="(total) => `共${total}条`"
            />
        </div>

        <OutsourcingCompanyForm  ref="formRef" @reloadList="queryData"/>

    </a-card>
    <a-modal v-model:visible="visible" width="800px" title="审批" @ok="handleOk">
        <a-form :labelCol="{span: 4}">
            <a-form-item label="审批结果">
                <a-radio-group v-model:value="approvalObj.type">
                <a-radio :value="1">通过</a-radio>
                <a-radio :value="2">不通过</a-radio>
                </a-radio-group>
            </a-form-item>
            <a-form-item label="不通过原因" v-if="approvalObj.type ===2">
                <a-textarea v-model:value="approvalObj.reason" placeholder="textarea with clear icon" allow-clear />
            </a-form-item>
        </a-form>
    </a-modal>
    <Details ref="detailsRef" />
</template>
<script setup>
    import { reactive, ref, onMounted } from 'vue';
    import { message, Modal } from 'ant-design-vue';
    import { SmartLoading } from '/@/components/framework/smart-loading';
    import {projectDailyReportApi} from "/@/api/business/oa/projectDailyReport";
    import { PAGE_SIZE_OPTIONS } from '/@/constants/common-const';
    import { smartSentry } from '/@/lib/smart-sentry';
    import TableOperator from '/@/components/support/table-operator/index.vue';
    import { TABLE_ID_CONST } from '/@/constants/support/table-id-const';
    import { useUserStore } from '/@/store/modules/system/user';
    import { useRoute, useRouter } from 'vue-router';
    import Details from './components/details.vue'
    // ---------------------------- 表格列 ----------------------------
    const router = useRouter();
  const route = useRoute();
    const columns = ref([
        {
            title: '序号',
            dataIndex: 'index',
            width: 50,
            align:'center'
        },
        {
            title: '项目名称',
            dataIndex: 'projectName',
            ellipsis: true,
            width:200,
        },
        {
            title: '项目地址',
            dataIndex: 'projectAddress',
            ellipsis: true,
            width:380,
        },
        {
            title: '工作日期',
            dataIndex: 'workDay',
            ellipsis: true,
            width:100,
        },
        {
            title: '登记人姓名',
            dataIndex: 'creatorName',
            ellipsis: true,
            width:100,
        },
        {
            title: '审批状态',
            dataIndex: 'approvalStatus',
            ellipsis: true,
            width:200,
        },
        {
            title: '审批未通过原因',
            dataIndex: 'reason',
            ellipsis: true,
            width:200,
        },
        {
            title: '操作',
            dataIndex: 'action',
            fixed: 'right',
            width: 135,
        },
    ]);

    // ---------------------------- 查询数据表单和方法 ----------------------------

    const queryFormState = {
        projectId: undefined, 
        workDay: undefined, 
        approvalStatus: undefined, //状态
        pageNum: 1,
        pageSize: 10,
    };
    // 查询表单form
    const queryForm = reactive({ ...queryFormState });
    // 表格加载loading
    const tableLoading = ref(false);
    // 表格数据
    const tableData = ref([]);
    // 总数
    const total = ref(0);

    // 重置查询条件
    function resetQuery() {
        let pageSize = queryForm.pageSize;
        Object.assign(queryForm, queryFormState);
        queryForm.pageSize = pageSize;
        queryData();
    }

    // 查询数据
    async function queryData() {
        tableLoading.value = true;
        try {
            let queryResult = await projectDailyReportApi.list(queryForm);
            tableData.value = queryResult.data.list;
            total.value = queryResult.data.total;
        } catch (e) {
            smartSentry.captureError(e);
        } finally {
            tableLoading.value = false;
        }
    }
    // 项目下拉
    const projectOptions = ref([])
    function getProject(){
        projectDailyReportApi.getProjectOptions().then(res=>{
            if(res.code===0){
                projectOptions.value = res.data.map(item=>{
                    return {
                        value:item.id,
                        label:item.projectName
                    }
                })
            }
        })
    }
    // 审批
    const visible = ref(false)
    const approvalObj = reactive({
        id:'',
        type:'',
        reason:''
    })
    function approval(val){
        console.log(val);
        visible.value = true;
        approvalObj.id = val.id
    }
    function handleOk(){
        console.log(approvalObj)
        if(approvalObj.type === 1){
            projectDailyReportApi.approve(approvalObj.id).then(res=>{
                if(res.code===0){
                    approvalObj.value = {id:'',type:'',reason:''};
                    visible.value = false;
                    queryData()
                }
            })
        }else if(approvalObj.type === 2){
            projectDailyReportApi.notApprove({
                id:approvalObj.id,
                reason:approvalObj.reason
            }).then(res=>{
                if(res.code===0){
                    approvalObj.value = {id:'',type:'',reason:''};
                    visible.value = false;
                    queryData()
                }
            })
        }
    }
    // 详情
    const detailsRef = ref(null)
    function detail(record){
        // router.push({ path: '/oa/projectDailyReport', query: {record:JSON.stringify(record)} });
        detailsRef.value.show(record)
        
    }
    function updata(record){
        router.push({ path: '/oa/projectDailyReport', query: {record:JSON.stringify(record)} });
    }
    onMounted(()=>{
        queryData();
        getProject();
    })

</script>
